package com.qh.feeds;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.songheng.eastnews.apk.R;

import java.util.ArrayList;

import cn.trinea.android.common.entity.FailedReason;
import cn.trinea.android.common.service.impl.ImageCache;
import cn.trinea.android.common.service.impl.ImageMemoryCache.OnImageCallbackListener;
import cn.trinea.android.common.util.CacheManager;
import cn.trinea.android.common.util.ObjectUtils;

public class FeedAdapter extends BaseAdapter {

    private static final String LOG_TAG = FeedAdapter.class.getSimpleName();
    private ArrayList<com.qh.feeds.FeedItem> list = new ArrayList<com.qh.feeds.FeedItem>();
    private Context context;
    private Activity activity;
    private ImageCache imageCache;


    public FeedAdapter(Context context, Activity activity) {
        this.context = context;
        this.activity = activity;

        imageCache = CacheManager.getImageCache();

        OnImageCallbackListener imageCallback = new OnImageCallbackListener() {

            @Override
            public void onGetFailed(String arg0, Bitmap arg1, View arg2,
                                    FailedReason arg3) {

            }

            @Override
            public void onGetNotInCache(String arg0, View arg1) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onGetSuccess(String arg0, Bitmap arg1, View arg2,
                                     boolean arg3) {
                if (arg2 != null && arg1 != null) {
                    ImageView imageView = (ImageView) arg2;
                    // add tag judge, avoid listView cache and so on
                    String imageUrlTag = (String) imageView.getTag();
                    if (ObjectUtils.isEquals(imageUrlTag, arg0)) {
                        BitmapDrawable bitmapDrawable = new BitmapDrawable(arg1);
                        imageView.setImageDrawable(bitmapDrawable);
                    }
                }
            }

            @Override
            public void onPreGet(String arg0, View arg1) {
                // TODO Auto-generated method stub

            }
        };

        imageCache.setOnImageCallbackListener(imageCallback);
    }

    public void addItem(com.qh.feeds.FeedItem item) {
        list.add(item);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    private View getAdView(final com.qh.feeds.FeedItem item, View convertView, ViewGroup parent) {
        RelativeLayout layout = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView != null) {
            if (convertView instanceof RelativeLayout) {
                layout = (RelativeLayout) convertView;
            }
        }
        if (layout == null) {
            layout = (RelativeLayout) inflater.inflate(R.layout.feedsaditem, null);
        }


        TextView titleTxt = (TextView) layout.findViewById(R.id.adtitle);

        if (titleTxt == null) {
            layout = (RelativeLayout) inflater.inflate(R.layout.feedsaditem, null);
            titleTxt = (TextView) layout.findViewById(R.id.adtitle);
        }

        TextView descTxt = (TextView) layout.findViewById(R.id.desc);
        ImageView iconView = (ImageView) layout.findViewById(R.id.icon);
        ImageView adimage = (ImageView) layout.findViewById(R.id.adimg);

        titleTxt.setText(item.title);
        descTxt.setText(item.desc);
        iconView.setTag(item.logo);
        iconView.setBackgroundColor(Color.TRANSPARENT);
        iconView.getBackground().setAlpha(0);

        imageCache.get(item.logo, iconView);

        adimage.setTag(item.adimg);
        imageCache.get(item.adimg, adimage);


        TextView btn = (TextView) layout.findViewById(R.id.btn);
        btn.setText(item.btntext);

        layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                item.ad.onAdClicked();
            }

        });
        if (!item.isShown) {
            item.ad.onAdShowed();
            item.isShown = true;
        }
        return layout;
    }

    private View getFeedView(final com.qh.feeds.FeedItem item, View convertView, ViewGroup parent) {
        LinearLayout layout = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView != null) {
            if (convertView instanceof LinearLayout) {
                layout = (LinearLayout) convertView;
            }
        }
        if (layout == null) {
            layout = (LinearLayout) inflater.inflate(R.layout.feedsitem, null);
        }

        TextView nameTxt = (TextView) layout.findViewById(R.id.name);

        if (nameTxt == null) {
            layout = (LinearLayout) inflater.inflate(R.layout.feedsitem, null);
        }
        nameTxt = (TextView) layout.findViewById(R.id.name);
        TextView contentTxt = (TextView) layout.findViewById(R.id.feedscontent);
        ImageView imageView = (ImageView) layout.findViewById(R.id.icon);

        nameTxt.setText(item.title);
        contentTxt.setText(item.desc);
        imageView.setTag(item.logo);
        imageView.setBackgroundColor(Color.TRANSPARENT);
        imageView.getBackground().setAlpha(0);

        imageCache.get(item.logo, imageView);

        layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                try {
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(item.link);
                    intent.setData(content_url);
                    activity.startActivity(intent);
                } catch (Exception e) {
                    Log.e(LOG_TAG, e.getMessage());
                }


            }

        });

        return layout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        com.qh.feeds.FeedItem item = (com.qh.feeds.FeedItem) this.getItem(position);

        if (item.isAd) {
            return getAdView(item, convertView, parent);
        } else {
            return getFeedView(item, convertView, parent);
        }

    }

}
